﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using WorkItemAnalyser.Core.Assertions;

namespace WorkItemAnalyser.Core
{
    public class Scenario : Query
    {
        private readonly IList<IAssertion> filters;

        public Scenario(string workItemType, string teamProjectName)
            : base(workItemType, teamProjectName)
        {
            filters = new Collection<IAssertion>();
        }

        public IEnumerable<IAssertion> FilterAssertions
        {
            get { return new Collection<IAssertion>(filters); }
        }

        public void AddFilter(IAssertion filter)
        {
            filters.Add(filter);
        }

        public override bool IsMatch(WorkItem workItem)
        {
            var isMatch = base.IsMatch(workItem);

            return filters.Select(assertion => assertion.Execute(new[] {workItem}))
                                .Where(results => results != null)
                                .SelectMany(results => results)
                                .Aggregate(isMatch, (current, result) => current & result.IsValid);
        }
    }
}
